define(function(require,exports,module){
	var aw = require("ajaxwrapper");
	var Select = require("select");
	var Dialog =require("dialog");
	var checkInRoomGridComp = require("../component/checkInRoomGrid");
	var checkInPerDetailGrid = require("../component/checkInPerDetailGrid");
	
	module.exports = {
		subnav: {
			handleReturnBtnClick: function(widget){
				widget.toList();
			},
			handleSaveBtnClick: function(widget){
				var ciiData = widget.get("curData");
				var carData = widget.get("ciiCarForm").getData();
				if(carData.haveCar == "true"){
					if(carData.address == "" || carData.carDate == ""){
						Dialog.alert({
							content: "地址或日期不能为空"
						});
						return false;
					}
					
				}
				carData.status = (carData.haveCar == "true" ? "Pending" : "NoRequiement");
				
//				if(dinnerData.haveDinner=="true"){
//       				if(dinnerData.breakfast || dinnerData.lunch || dinnerData.supper){
//       					
//       				}else{
//       					Dialog.alert({
//       						content:"请选择 早餐，午餐或晚餐！"
//       					});
//       					return;
//       				}
//       				
//       			}
//				dinnerData.status = (dinnerData.haveDinner == "true" ? "Pending" : "NoRequiement");
				
				
				var cParams = {
					pkCIImplement: ciiData.pkCIImplement,
					version: ciiData.version,
					checkInCar: carData,
					dinners:[] ,
					cRoomPersonalInfos: [],
					signUpRooms:[]
				};
				var dinnerData = widget.get("ciidinnergrid").getData();
				
				// 循环餐饮类型，添加Version默认值
				var dinner=[];
				if(dinnerData.length>0){
					
					for (var i = 0; i < dinnerData.length; i++) {
						dinnerData[i].version=0;
						dinner.push(dinnerData[i])
					}
				}
				
				cParams.dinners.push({
					dinners:dinner
				})
				
				if(widget.get("dataSource") == "GroupReserve"){
					var roomData = widget.get("checkInRoomGrid").getData();
					var roomMap = {};
					var travelContractMap = {};
					for(var i=0;i<roomData.length;i++){
						if(!roomData[i].roomRegister){
							Dialog.alert({
								content: "请分配房间"
							});
							return false;
						}
						var roomRegister = roomData[i].roomRegister.pkRoomRegister;
						cParams.cRoomPersonalInfos.push({
							pkRoomPersonalInfo: roomData[i].pkRoomPersonalInfo,
							descript:roomData[i].descript,
							version: roomData[i].version || 0,
							pkCIImplement: ciiData.pkCIImplement,
							roomRegister: roomRegister,
							signUpRegister: roomData[i].signUpRegister.pkSignUpRegister,
							checkIn:false
						});
						
						if(roomData[i].signUpRegister.travelContract){
							if(roomMap[roomData[i].roomRegister.pkRoomRegister]){
								roomMap[roomData[i].roomRegister.pkRoomRegister].push(roomData[i].signUpRegister.travelContract.pkTravelContract);
							}else{
								roomMap[roomData[i].roomRegister.pkRoomRegister] = [];
								roomMap[roomData[i].roomRegister.pkRoomRegister].push(roomData[i].signUpRegister.travelContract.pkTravelContract);
							}
							for(var key in roomMap){
								$.unique(roomMap[key]);
							}
							
							var pkTravelContract = roomData[i].signUpRegister.travelContract.pkTravelContract;
							travelContractMap[pkTravelContract] = {
									roomNum:roomData[i].signUpRegister.travelContract.productRule.roomNum
							};
						}
						
					}
					// 判断权证可落实的房间数量
					if(travelContractMap){
						for(var key in travelContractMap){
							var num = 0;
							for(var j in roomMap){
								if($.inArray(parseInt(key),roomMap[j])>=0){
									num++;
								}
							}
							
							if(travelContractMap[key].roomNum !=null && travelContractMap[key].roomNum < num){
								Dialog.alert({
									tip: "提示！",
									content: "VIP的统一权证只能最多选择"+ travelContractMap[key].roomNum +"个房间"
								});
								return false;
							}
						}
					}
				}else{
					var signUpRoomData = widget.get("roomTypeEditGrid").getData();
					for(var i=0; i < signUpRoomData.length; i++){
						if(signUpRoomData[i].roomType){
							signUpRoomData[i].roomType = signUpRoomData[i].roomType.pkRoomType;
						}
						if(typeof signUpRoomData[i].roomStatus != "string"){
							signUpRoomData[i].roomStatus = signUpRoomData[i].roomStatus.key;
						}
					}
					cParams.signUpRooms = signUpRoomData;
				}
				widget.service.save(aw.customParam(cParams),function(){
					widget.get("grid").refresh();
					widget.toList();
				});
			}
		},
		grid: {
			handleSetBtnClick: function(widget,data,flag){
				var dataSource = data.dataSource.key;
				widget.set("dataSource", dataSource);
				widget.toCard();
				if(dataSource == "GroupReserve"){
					checkInRoomGridComp.initComponent(null, widget,data.groupReserve?data.groupReserve.roomRegisters:null);
					widget.set("dataSource", "GroupReserve");
					aw.ajax({
						  url:"api/checkinimplement/queryroompersonal",
						  data:{
							  "pkCIImplement":data.pkCIImplement,
							  fetchProperties:"signUpRegister.personalInfo.pkPersonalInfo," +
							  	"signUpRegister.exitGroup," +
							  	"signUpRegister.travelContract.productRule.roomNum," +
							  	"signUpRegister.travelContract.pkTravelContract," +
	      						"signUpRegister.personalInfo.name," +
	      						"signUpRegister.personalInfo.sex.value," +
	      						"signUpRegister.personalInfo.idNumber," +
	      						"signUpRegister.personalInfo.mobilePhone," +
	      						"signUpRegister.pkSignUpRegister," +
	      						"roomRegister.pkRoomRegister," +
	      						"roomRegister.room.*," +
	      						"roomRegister.room.orgBuilding.pkOrgBuilding," +
	      						"roomRegister.room.orgBuilding.name," +
	      						"pkRoomPersonalInfo,descript"
						  },
						  dataType:"json", 
						  success:function(ret){
							  var checkInRoomGrid = widget.get("checkInRoomGrid");
							  checkInRoomGrid.setData(ret);
							  
							  for(var i=0;i<ret.length;i++){
								  if(flag){
									  widget.get("subnav").hide(["save"]);
									  widget.get("ciiCarForm").setDisabled(true);
									  widget.get("ciiDinnerForm").setDisabled(true);
									  widget.get("ciiBaseForm").setDisabled(true);
								  }
							  }
						  }
					  });
					widget.get("ciiBaseForm").setData({
						name: data.groupReserve.planItemGroup.allianceOrg ? 
								data.groupReserve.planItemGroup.allianceOrg.name : data.groupReserve.planItemGroup.servicePoint.name,
						destination: data.groupReserve.planItemGroup.planItem.destination.name,
						checkInDate: data.groupReserve.checkInDate,
						checkOutDate: data.groupReserve.checkOutDate,
						signUpNum: data.signUpNum
					});
				}else{
					widget.get("ciiBaseForm").setData({
						name: data.nonMemGroupReserve.nonMemberGroup.contactName,
						destination: data.nonMemGroupReserve.nonMemberGroup.servicePoint.name,
						checkInDate: data.nonMemGroupReserve.nonMemberGroup.checkInDate,
						checkOutDate: data.nonMemGroupReserve.nonMemberGroup.checkOutDate,
						signUpNum: data.nonMemGroupReserve.nonMemberGroup.perNumber
					});
					if(data.nonMemGroupReserve.nonMemberGroup.signUpType && 
							data.nonMemGroupReserve.nonMemberGroup.signUpType.key == "Experience"){
						widget.get("ciiBaseForm").setValue("signUpNum", data.signUpNum);
					}
				}
				var carForm = widget.get("ciiCarForm");
				$("#roomPersonalInfos").removeClass("hidden");
				widget.set("curData",data);
				carForm.reset();
				if(data.checkInCar!=null && data.checkInCar.status.key=="Pended"){
					carForm.show(["haveCar","address","carDate","description","corpName","driverName","driverPhone","car","carNumber",]);
					carForm.setDisabled(true);
					carForm.setValue("haveCar",data.checkInCar.haveCar);
					carForm.setValue("address",data.checkInCar.address);
					carForm.setValue("carDate",data.checkInCar.carDate);
					carForm.setValue("description",data.checkInCar.description);
					for(var i = 0;i < data.carDispatchBill.length;i++){
						if(data.carDispatchBill[i].unitsCar){
							carForm.hide(["corpName"]);
							carForm.setValue("car",data.carDispatchBill[i].car.carType);
							carForm.setValue("carNumber",data.carDispatchBill[i].car.carNumber);
							carForm.setValue("driverName",data.carDispatchBill[i].driver.personalInfo.name);
							carForm.setValue("driverPhone",data.carDispatchBill[i].driver.personalInfo.phone);
						}else{
							carForm.hide(["carNumber"]);
							carForm.setValue("corpName",data.carDispatchBill[i].companyName);
							carForm.setValue("car",data.carDispatchBill[i].carModel);
							carForm.setValue("driverName",data.carDispatchBill[i].driverName);
							carForm.setValue("driverPhone",data.carDispatchBill[i].driverPhone);
						}
					}
				}else{
					carForm.hide(["corpName","driverName","driverPhone","car","carNumber"]);
					widget.get("ciiCarForm").setData(data.checkInCar || {});
				}
			  	widget.get("roomTypeEditGrid").setData(data.nonMemGroupReserve==null?{}:data.nonMemGroupReserve.signUpRooms);
			  	$(".J-grid-columns-descript").eq(0).click();
//			  	widget.get("ciiDinnerForm").reset();
//			  	widget.get("ciiDinnerForm").setData(data.checkInDinner || {});
			},
			handleDetailBtnClick: function(widget,data){
				this.handleSetBtnClick(widget,data);
			},
			handlePreBtnClick: function(widget,data){
				widget.service.prepare(data,function(){
					widget.get("grid").refresh();
				});
			}
		}
	};
})
